<?php
ob_start();
define('LOUBIA_ZONE',true);

include_once('config.inc.php');

header('Content-Type: text/html; charset=' . $lb['charset']);

if ($lb['debug']){
	error_reporting(E_ALL);
}else{
	error_reporting(E_NONE);
}

include_once($lb['dir'] . 'include/calendar.inc.php');
include_once($lb['dir'] . 'include/calendar/' . $lb['calendar'] . '.inc.php');
include_once($lb['dir'] . 'include/db/' . $lb['db_type'] . '.inc.php');
include_once($lb['dir'] . 'include/db.inc.php');
include_once($lb['dir'] . 'include/session.inc.php');
//include_once($lb['dir'] . 'include/cron.inc.php');
include_once($lb['dir'] . 'include/system.inc.php');
include_once($lb['dir'] . 'include/group.inc.php');
include_once($lb['dir'] . 'include/page.inc.php');
include_once($lb['dir'] . 'include/plugin.inc.php');
include_once($lb['dir'] . 'include/user.inc.php');
include_once($lb['dir'] . 'include/Smarty/libs/Smarty.class.php');
include_once($lb['dir'] . 'include/smarty.inc.php');
include_once($lb['dir'] . 'language/' . $lb['lang'] . '.inc.php');

// load english language (default)
if($lb['lang'] != 'en'){
	include_once($lb['dir'] . 'language/en.inc.php');
}

$lb['calendar']	= new $lb['calendar'];
$lb['db'] 		= new lb_db();
$lb['session'] 	= new lb_session();
//$lb['cron'] 	= new lb_cron();
$lb['user'] 	= new lb_user();
$lb['smarty'] 	= new lb_smarty();

$lb['current_dir']		 = $_SERVER['PATH'];
$lb['current_file']		 = $_SERVER['PHP_SELF'];
$lb['current_full_file'] = $_SERVER['REQUEST_URI'];

if(isset($_GET['do'])){
	$lb['do'] = strtolower (trim ($_GET['do']));
}else{
	$lb['do'] = '';
}

if(isset($_GET['action'])){
	$lb['action'] = strtolower (trim ($_GET['action']));
}else{
	$lb['action'] = '';
}

if(isset($_GET['title'])){
	if($page_id_tmp = lb_page::check_title(urldecode(trim($_GET['title'])))){
		$_GET['page'] = $page_id_tmp;
	}else{
		$lb['error'][] = LANG_PAGE_ERROR_EXIST;
		$lb['task']	   = 'error';
	}
}


switch($lb['do']){
	case 'search':
		$lb['page_title'] = LANG_SEARCH;
		$page_info = false;
		$lb['smarty']->assign ('search', true);
		
		if(isset($_GET['q'])){
			$search_query	= lb_system::htmlspecialchars($_GET['q']);
			$qs 			= lb_system::search_query($_GET['q']);
			$lb['smarty']->assign ('search_query', $search_query);
		}else{
			$qs = false;
		}
		
		if($qs){
			
			//Setting search page number
			if(isset($_GET['search_page'])){
				$search_page = abs($_GET['search_page']);
			}else{
				$search_page = 1;
			}
			
			//Search in page title or content
			if(isset($_GET['search_in']) && trim($_GET['search_in']) == 'title'){
				$search_in = 'title';
			}else{
				$search_in = 'all';
			}
			
			//Determine search do in selected page type or not
			if(isset($_GET['type'])){
				$search_page_type = trim($_GET['type']);
			}else{
				$search_page_type = $lb['default_type'];
			}

			$search_start = ($search_page - 1) * $lb['page_results'];
			$search_result = lb_page::search($search_page_type, $qs, $search_start,
										     $lb['page_results'], $search_in);
			
			if($search_result['num_of_rows'] > 0){
				$search_pages_num = ceil ($search_result['num_of_rows'] / $lb['page_results']);
				
				$lb['smarty']->assign ('search_num'	 , $search_result['num_of_rows']);
				$lb['smarty']->assign ('search_query' 	 , $search_query);
				$lb['smarty']->assign ('search_page'	 , $search_page);
				$lb['smarty']->assign ('search_pages_num', $search_pages_num);
			//if the search did NOT have any result
			}
			
			$lb['smarty']->assign ('search_in'	 , $search_in);
			$lb['smarty']->assign ('search_page_type', $search_page_type);
			$lb['smarty']->assign ('search_results'	 , $search_result['rows']);
			
		}else{
			$lb['smarty']->assign ('new_search', true);
		}
		
		$sys_pages  = lb_page::list_pages();
		$lb['smarty']->assign('sys_pages', $sys_pages);
		$_GET['page'] = 0;
		break;
	case 'login':
		if(isset($_POST['login']) && isset($_POST['username']) 
		&& isset($_POST['password']) && trim($_POST['username'])!='' && trim($_POST['password'])!=''){
			  
		    if(isset($_POST['remember']) && $_POST['remember'] == 'on'){
			    $rem = true;
			}else{
				$rem = false;
			}
			  
			if($lb['user']->login ($_POST['username'], $_POST['password'], $rem)){
				$lb['redirect'] = LANG_LOGGED_IN;
			}else{
				$lb['redirect'] = LANG_INVALID_USER_OR_PASS;
			}
			$_REQUEST['redirect'] = $lb['webdir'];
			$_GET['page'] = 0;
		}else{
			$lb['do'] = '';
		}	
		break;
		
	case 'logout':
		// if user logged out
		if(isset($_POST['do']) && $_POST['do'] == 'logout'){
			$lb['user']->logout();
			$_REQUEST['redirect'] = $lb['webdir'];
			$lb['redirect'] = LANG_LOGGED_OUT;
			$_GET['page'] = 0;
		}else{
			$lb['do'] = '';
		}
		break;  
	 
	default:
		$lb['do'] = '';  
		//$lb['action'] = '';
}


if(!$lb['user']->logged() && isset($_COOKIE[$lb['cookie']])){	
	//if cookie had been set
	$lb['user']->login_from_cookie();
}

$lb['page'] = new lb_page();

// set stylesheet CSSes
$lb['css'] 		= $lb['template_dir'] . $lb['page']->template . '/' . $lb['page']->css;
$lb['print_css'] 	= $lb['template_dir'] . $lb['page']->template . '/' . $lb['page']->print_css;

/////////////////////////////PAGE PLUGIN INIT\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\START
// include file of desired type of page
if($lb['page']->type != false){
	$type = 'type_' . $lb['page']->type;
	include_once($lb['dir'] . 'page/' . $type . '/' . $type . '.inc.php');

	// make a new instance of the desired type of page (!)
	$lb['cpage'] = new $type();
//if request is a valid page
}elseif($lb['page']->page > 0){
	$lb['cpage'] = false;
	lb_system::runaway (LANG_PAGE_NO_PAGE);
	$lb['task'] = 'error';
}else{
	$lb['cpage'] = false;
}
/////////////////////////////PAGE PLUGIN INIT\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\FINISH


/////////////////////////PAGE PERMISSION CONTROL\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\START

// if user can READ/WRITE this page
if($lb['user']->is_su() || $lb['user']->is_moderator() || !$lb['page']->type){
	$lb['read']		= true;
	$lb['write']	= true;
}else{
	// get/set permissions
	$lb['user']->get_permissions();
	$lb['read']		= $lb['user']->can_read();
	$lb['write']	= $lb['user']->can_write();
}

$lb['smarty']->assign ('write', $lb['write']);

// if page is inactive
if($lb['page']->active == false){
	lb_system::runaway (LANG_THIS_PAGE_IS_INACTIVE);
}

// if user cannot read the page
if(!$lb['read']){
	lb_system::runaway (LANG_DONT_HAVE_READ_PERMISSION);
	$lb['task'] = 'error';
}
/////////////////////////PAGE PERMISSION CONTROL\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\FINISH

//////////////////////////////LOAD PLUGINS\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\START
$lb['plugin'] = new lb_plugin();
$lb['plugin']->get_content();
//////////////////////////////LOAD PLUGINS\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\FINISH

switch ($lb['action']){
	
	case 'add':
		if($lb['write'] == false){
			lb_system::runaway (LANG_DONT_HAVE_WRTIE_PERMISSION);
			break;
		}
		
		$sys_pages = lb_page::list_pages();
		
		if(isset($_GET['type'])){
			$lb['page_type'] = trim($_GET['type']);
			if(!array_key_exists($lb['page_type'], $sys_pages)){
				$lb['error'][] = 'Type not found!';
				$lb['task']    = 'error';
				break;
			}else{
				$lb_classes = lb_system::get_declared_classes();
				if(in_array($sys_pages[$lb['page_type']]['class'], $lb_classes) == false){
					include($lb['dir'] . 'page/' . $sys_pages[$lb['page_type']]['path'] . '/' .
					 $sys_pages[$lb['page_type']]['class'] . '.inc.php');
				}
			}
			$lb['smarty']->assign ('add_page_type'	, $lb['page_type']);
		}else{
			$lb['task'] = 'add';
			$lb['smarty']->assign ('page_types'	, $sys_pages);
			$menu_close_link	= $lb['webdir'] . 'index.php?page=' . $lb['page']->page;
			$lb['smarty']->assign ('menu_close_link', $menu_close_link);
			break;
		}
		
		if(isset($_POST['write']) && $_POST['write'] == 'on'){
			$write_perm	= true;
		}else{
			$write_perm	= false;
		}
		
		if(isset($_POST['title'])){
			$_POST['title'] = lb_system::htmlspecialchars($_POST['title']);
			$add_page_id	= false;
			$add_parent_id	= false;
			eval('$page_id = ' . $sys_pages[$lb['page_type']]['class'] . '::add_page(array_merge($_POST, $_FILES));');
			//$page_id = $lb['cpage']->add_page ($_POST);
			
			if($page_id != false){
				if(!($result = $lb['page']->add_page ($lb['page_type'], $page_id, $_POST['title'],
				 				$write_perm, $add_page_id, $add_parent_id))){
					$lb['cpage']->remove_page($page_id);
				}
				
			}else{
				$result = false;
			}
			$lb['task'] = 'error';
			if($result){
				$lb['redirect'] = LANG_ADDED_PAGE;
				$_REQUEST['redirect'] = $lb['webdir'] . '?page=' . $result;
			}else{
				$lb['error'][] = LANG_ADD_PAGE_ERR;
				$lb['error']   = array_merge($lb['error'], $lb['page']->get_errors());
				$lb['task']    = 'error';
			}
		}else{
			//if user only clicked on "add page" link
			$menu_close_link 	= $lb['webdir'] . 'index.php?page=' . $lb['page']->page;
			
			$lb['smarty']->assign ('add_page', true);
			$lb['smarty']->assign ('menu_close_link', $menu_close_link);
			//$lb['content']	= false;
		}
		
		
		break;
		
	case 'edit':
		if($lb['write'] == false){
			lb_system::runaway (LANG_DONT_HAVE_WRTIE_PERMISSION);
			break;
		}
		
		if(isset($_POST['title'])){	
			$_POST['title'] = lb_system::htmlspecialchars($_POST['title']);
			if($lb['cpage']->edit_page (array_merge($_POST, $_FILES)) != false){
				if(!($result	= $lb['page']->edit_page ($_POST['title']))){
					$lb['cpage']->restore();
				}
			}else{
				$result = false;
			}
			
			if($result == true){
				$lb['message'][] = LANG_EDITED;
				$lb['content']	= $lb['cpage']->get_content();
				$lb['task'] 	= 'content';
			}else{
				$lb['error'][] 	= LANG_EDIT_ERR;
				$lb['error']	= array_merge($lb['error'], $lb['page']->get_errors());
				$lb['task'] = 'error';
			}

		}else{
			//if user only clicked on "edit" link
			$menu_close_link 	= $lb['webdir'] . 'index.php?page=' . $lb['page']->page;
			$lb['content']		= $lb['cpage']->get_content(false);				
			$lb['smarty']->assign ('menu_close_link', $menu_close_link);
		}
		break;
		
	case 'move':
		if($lb['write'] == false){
			lb_system::runaway (LANG_DONT_HAVE_WRTIE_PERMISSION);
			break;
		}
		if(isset($_POST['confirm']) && isset($_POST['pid'])){
			$new_pid = intval ($_POST['pid']); 
			$result  = $lb['page']->move_page ($new_pid);
			
			if($result == true){
				lb_system::redirect ('index.php?page=' . $lb['page']->page, LANG_MOVED_PAGE);
			}else{
				$lb['error'][] 	= LANG_MOVE_PAGE_ERR;
				$lb['content']	= $lb['cpage']->get_content();
			}	
			
		}else{
			//$lb['content']	 	= NULL;
			$menu_close_link	= $lb['webdir'] . 'index.php?page=' . $lb['page']->page;
			$lb['smarty']->assign ('menu_close_link', $menu_close_link);
		}
		break;
		
	case 'remove':
		if($lb['write'] == false){
			lb_system::runaway (LANG_DONT_HAVE_WRTIE_PERMISSION);
			break;
		}
		
		if(isset($_POST['confirm'])){
			$lb['cpage']->remove_page();
			$result = $lb['page']->remove_page();
			
			if($result == true){
				lb_system::redirect ('index.php?page=' . $lb['page']->pid, LANG_REMOVED_PAGE);
			}else{
				$lb['error'][] 	= LANG_REMOVE_PAGE_ERR;
				$lb['content']	= $lb['cpage']->get_content();
				$lb['task']  	= 'error';
			}
			
			
		}else{
			//$lb['content']		= null;
			$menu_close_link	= $lb['webdir'] . 'index.php?page=' . $lb['page']->page;
			$lb['smarty']->assign ('menu_close_link', $menu_close_link);
		}
		break;
		
	case 'change_type':
		if($lb['write'] == false){
			lb_system::runaway (LANG_DONT_HAVE_WRTIE_PERMISSION);
			break;
		}
		
		$sys_pages  = lb_page::list_pages();
		if(isset($_GET['type'])){
			$new_page_type = trim($_GET['type']);
			if(!array_key_exists ($new_page_type, $sys_pages)){
				$lb['error'][] = LANG_TYPE_ERROR_EXIST;
				$lb['task']    = 'error';
				break;
			}else{
				$lb_classes = lb_system::get_declared_classes();
				if(in_array($sys_pages[$new_page_type]['class'], $lb_classes) == false){
					include($lb['dir'] . 'page/' . $sys_pages[$new_page_type]['path'] . '/' . $sys_pages[$new_page_type]['class'] . '.inc.php');
				}
			}
			$lb['smarty']->assign ('new_page_type', $new_page_type);
		}else{
			$lb['smarty']->assign ('sys_pages', $sys_pages);
			$menu_close_link	= $lb['webdir'] . 'index.php?page=' . $lb['page']->page;
			$lb['smarty']->assign ('menu_close_link', $menu_close_link);
			break;
		}
		
		if(isset($_POST['submit'])){
			$_POST['title'] = $lb['cpage']->get_title();
			
			eval('$new_page_type_id = ' . $sys_pages[$new_page_type]['class'] . '::add_page(array_merge($_POST, $_FILES));');;
					
			if($lb['page']->edit_type($lb['page']->page, $new_page_type, $new_page_type_id)){
				$lb['cpage']->remove_page();
				lb_system::redirect('index.php?page=' . $lb['page']->page, LANG_TYPE_CHANGED);
			}else{
				$lb['cpage']->remove_page($new_page_type_id);
				$lb['error'][] = 'Error in page type changing (page update)!';
				$lb['task']	   = 'error';
			}
			
		}else{
			$menu_close_link	= $lb['webdir'] . 'index.php?page=' . $lb['page']->page;
			$lb['smarty']->assign ('menu_close_link', $menu_close_link);
		}
		
		break;
	
	default:
		if(!isset($lb['task'])){
			$lb['task']	= 'content';
		}
		if($lb['cpage'] != false){
			$lb['content']	  = $lb['cpage']->get_content();
		}
		break;
}

if($lb['page']->page > 0){
	 // set/generate title and menu items
	$lb['page_menu'] = $lb['page']->generate_menu();
	$lb['smarty']->assign ('page_menu' , $lb['page_menu']); 
}

// redirect page if SET
if(isset($_REQUEST['redirect'])){
	if(!isset($lb['redirect'])){
		$lb['redirect'] = LANG_REDIRECT;
	}
	lb_system::redirect($_REQUEST['redirect'], $lb['redirect']);
}

////////////////////////////////////////////////////////////////////////////////
if(isset($lb['do'])){
	$lb['smarty']->assign ('do' , $lb['do']);
}
if(isset($lb['task'])){
	$lb['smarty']->assign ('task'	, $lb['task']);
}
if(isset($lb['action'])){
	$lb['smarty']->assign ('action' , $lb['action']);
}

if($lb['cpage'] != false && !isset($lb['page_title'])){
	$lb['page_title'] = $lb['cpage']->title;
}elseif(!isset($lb['page_title'])){
	$lb['page_title'] = 'No page';
}

if(isset($lb['content'])){
	$lb['change_log'] = lb_page::get_change_log($lb['page']->page, 1);
	$lb['smarty']->assign('change_log'	, $lb['change_log']);
	$lb['smarty']->assign('content'	, $lb['content']);
}

if(!isset($lb['page_type'])){
	$lb['page_type'] = $lb['page']->type;
}

$lb['smarty']->assign ('script'		, $lb['current_file']);
$lb['smarty']->assign ('is_user'		, $lb['user']->is_user());
$lb['smarty']->assign ('username'		, $lb['user']->username);
$lb['smarty']->assign ('parent_id'		, $lb['page']->pid);
$lb['smarty']->assign ('write'		, $lb['write']);
$lb['smarty']->assign ('page'			, $lb['page']->page);
$lb['smarty']->assign ('webdir'		, $lb['webdir']);
$lb['smarty']->assign ('dir'			, $lb['dir']);
$lb['smarty']->assign ('page_type'		, $lb['page_type']);
$lb['smarty']->assign ('language'		, $lb['lang']);
$lb['smarty']->assign ('logged'		, $lb['user']->logged());
$lb['smarty']->assign ('css'			, $lb['css']);
$lb['smarty']->assign ('print_css'		, $lb['print_css']);
$lb['smarty']->assign ('message'		, $lb['message']);
$lb['smarty']->assign ('error'		, $lb['error']);
$lb['smarty']->assign ('title'		, $lb['page_title']);
$lb['smarty']->assign ('template_dir'   , $lb['template_dir'] . $lb['template']);
$lb['smarty']->display('index.tpl');
?>
